package demo1;
public class MyQueue {
    private Integer[] arr;
    private int size;
    private int head;
    private int tail;
    public MyQueue(int initialCapacity){
        arr=new Integer[initialCapacity];
    }
    public void put(Integer value) throws InterruptedException {
        synchronized (this) {
            while (size == arr.length) {
                this.wait();
            }
            arr[tail] = value;
            tail++;
            size++;
            if (tail == arr.length) {
                tail = 0;
            }
            this.notifyAll();
        }
    }
    public synchronized Integer take() throws InterruptedException {
        while (size==0) {
            this.wait();
        }
        Integer temp=arr[head];
        head++;
        size--;
        if(head== arr.length) head=0;
        this.notifyAll();
        return temp;
    }
}
